/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VISIE.Games;

import Basketball.GameManager;
import VISIE.InitialSettings;
import VISIE.characters.BasketballAgent;
import VISIE.characters.BasketballCharacter;
import VISIE.characters.BasketballTeam;
import VISIE.characters.Character;
import VISIE.mathfunctions.Conversions;
import VISIE.scenemanager.Court;
import VISIE.scenemanager.SceneCharacterManager;
import java.util.ArrayList;

/**
 *
 * @author Divesh
 */
public class PickupGame extends Game{
    
    private BasketballCharacter previousCharacterTouch = null;
    private int timeLimitSeconds = -1;
    private boolean gameOver = false;
    
    public PickupGame(){
    }
    
    public void createTeamsAndAgents(){
       
       for(int i = 0; i < numberOfTeams; i++){
           
           int agentsInTeam;
           
           if(i == playerTeamID){
               agentsInTeam = membersPerTeam - numberOfNUPs - 1;
           }
           else{
                agentsInTeam = membersPerTeam;
           }
           
           if(agentsInTeam < 0){ //adjust for NUPs
               agentsInTeam = 0;
           }
           
           BasketballTeam team = new BasketballTeam(agentsInTeam, characterCreator, i);

           if(team.getTeamID() == playerTeamID){
               p.setTeam(team);
               team.addToTeam(p);
           }  

           ArrayList<Character> c = team.initialisePlayers(i, ball);
           for(int j = 0; j < c.size(); j++){
               characterArray.add(c.get(j));
           }
           
           teams.add(team);
           if(team.getTeamID() == 1){
               team.setAttacking();
           }
       }
       
       //sets possession character
       BasketballCharacter startPossessionCharacter;
       if(numberOfTeams > 1){
            startPossessionCharacter = (BasketballCharacter)teams.get(1).getRandomTeamMate();
       }
       else{
           startPossessionCharacter = p;
       }
       startPossessionCharacter.setPossession();
       
      if(startPossessionCharacter instanceof VISIE.characters.BasketballAgent){
           BasketballAgent ba = (BasketballAgent)startPossessionCharacter;
           ba.setBehaviorState(0);
       }
       
      //set initial facing directions
       for(int i = 0; i < characterArray.size(); i++){
           BasketballCharacter bc = (BasketballCharacter)characterArray.get(i);
           
           if(bc instanceof VISIE.characters.BasketballAgent){
               BasketballAgent ba = (BasketballAgent)bc;
               float initRot;
               
               if(startPossessionCharacter.getTeamID() == ba.getTeamID()){ 
                   initRot = Conversions.originToTargetAngle(ba.getPosition(), Court.getHoopLocation());
               }
               else{
                   initRot = Conversions.originToTargetAngle(ba.getPosition(), SceneCharacterManager.getCharacterInPossession().getPosition());
               }
               ba.abo.setFacingDirection(initRot, initRot);
           }
           
       }
   }
    
    public void initializeGlobalSettings(){
          membersPerTeam = initialSettings.getNumPlayers();
          numberOfTeams = 2;
          if(initialSettings.getOffense()){
              playerTeamID = 1;
          }
          else{
              playerTeamID = 0;
          }
      }
    
     public void customizeEnvironment(){
     
     }
     
     @Override
     public void updateGame(float tpf){
         if(!gameOver){
            super.updateGame(tpf);
            this.updateTeamInformation();
            this.checkPassing();
            gsm.doGameStateManagement(guiNode);
            gsm.displayInstructions(guiNode);
            if(timeLimitSeconds >= 0 && timeRemaining >= 0){
                String timeRemainingString = Conversions.secondsConversionString(this.getTimeRemaining());
                gsm.setTimeText(timeRemainingString, guiNode);
            }
            else if(timeRemaining < 0){
                gsm.setTimeText("GAME OVER", guiNode);
                gameOver = true;
            }
         }
     }
     
    private void updateTeamInformation(){
      
      if(SceneCharacterManager.getCharacterByID(ball.getLastTouched()) != null){
            Character c = SceneCharacterManager.getCharacterByID(ball.getLastTouched());
            for(int i = 0; i < teams.size(); i++){
                if(teams.get(i).isMember(c)){
                    teams.get(i).setAttacking();
                }
                else{
                    teams.get(i).setDefending();
                }
            }
      }
  }
    
    private void checkPassing(){
        if(GameManager.getGameState() == 0){
            BasketballCharacter posChar = SceneCharacterManager.getCharacterInPossession();
            if(posChar != null){
                if(previousCharacterTouch == null){
                    System.out.println("one touch");
                    previousCharacterTouch = posChar;
                    
                }
                else if(previousCharacterTouch != posChar && posChar.playerIsTeamMate(previousCharacterTouch)){
                    System.out.println("two touch");
                    previousCharacterTouch = posChar;
                    gsm.setTwoTouch(true);
                }           
            }
        
        }
        else{
            gsm.setTwoTouch(false);
            previousCharacterTouch = null;   
        }
    } 
    
    private float getTimeRemaining(){
        Float timeElapsed = Conversions.nanoToSecond(System.nanoTime() - timeGameStarted); 
        timeRemaining = timeLimitSeconds - timeElapsed.intValue();
        return timeLimitSeconds - timeElapsed;
    }
    
    public void setTimeLimit(int seconds){
        timeLimitSeconds = seconds;
    }
    
}
